package org.BUPT.api.controller;

import lombok.extern.log4j.Log4j2;
import org.BUPT.application.service.AnswerService;
import org.BUPT.application.service.MessageService;
import org.BUPT.domain.model.message.AnswerLiked;
import org.BUPT.domain.model.message.QuestionAnswered;
import org.BUPT.domain.model.question.AnswerBO;
import org.BUPT.infrastructure.exception.NullObjectException;
import org.BUPT.infrastructure.exception.PersistException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
@Log4j2
@CrossOrigin
public class AnswerServiceController {
    @Autowired
    AnswerService answerService;

    /**
     * 用户给一个回答点赞
     * @param userId: 点赞的用户id
     * @param answerId: 被点赞的回答id
     */
    @PostMapping("/likeAnswer")
    public void likeAnswer(
            @RequestParam(value = "userId") int userId,
            @RequestParam(value = "answerId") int answerId
    ){
        try
        {
            answerService.likeAnswer(userId,answerId);
        }
        catch (Exception e) {
            log.info(e.getMessage());
        }
    }

    /**
     * 用户取消给一个回答点赞
     * @param userId: 取消点赞的用户id
     * @param answerId: 被取消点赞的回答id
     */
    @PostMapping("/dislikeAnswer")
    public void dislikeAnswer(
            @RequestParam(value = "userId") int userId,
            @RequestParam(value = "answerId") int answerId
    ){
        try
        {
            answerService.dislikeAnswer(userId,answerId);
        }
        catch (Exception e) {
            log.info(e.getMessage());
        }
    }

    /**
     * 用户查看自己是否给某个回答点赞
     * @param userId: 查看点赞的用户id
     * @param answerId: 回答的id
     * @return 返回是否点过赞
     */
    @PostMapping("/answerIsLikedByUser")
    public boolean isAnswerLiked(
            @RequestParam(value = "userId") int userId,
            @RequestParam(value = "answerId") int answerId
    ) {
        try{
            return answerService.isLiked(userId, answerId);
        }
        catch (Exception e){
            log.info(e.getMessage());
            return false;
        }
    }

    /**
     * 查看一个回答的总赞数
     * @param answerId: 回答id
     * @return 被赞总数
     */

    @PostMapping("/getLikeCount")
    public int getLikeCount(
            @RequestParam(value = "answerId") int answerId
    ){
        try {
            return answerService.getAnswer(answerId).getLikedUserId().size();
        }
        catch (Exception e){
            log.info(e.getMessage());
            return -1;
        }
    }

    /**
     * POST
     * 用户回答一个问题
     * @param userId: 回答者的id
     * @param questionId: 所回答的问题id
     * @param content: 回答的内容
     */
    @PostMapping("/addAnswer")
    public void addAnswer(
            @RequestParam(value = "userId") int userId,
            @RequestParam(value = "questionId") int questionId,
            @RequestParam(value = "content") String content
    ){
        try{
            answerService.createAnswer(userId,questionId,content);
        }
        catch (Exception e){
            log.info(e.getMessage());
        }
    }

    /**
     * POST
     * 用户修改一个回答
     * @param answerId: 所修改的回答id
     * @param content: 修改后的回答内容
     */
    @PostMapping("/modifyAnswer")
    public void modifyAnswer(
            @RequestParam(value="answerId") int answerId,
            @RequestParam(value = "content") String content
    ){
        try{

            answerService.modifyAnswer(answerId,content);
        }
        catch (Exception e){
            log.info(e.getMessage());
        }

    }

}
